package turtle.system.doug.turtleaction;

import java.util.ArrayList;

import turtle.system.pt.Bar;
import turtle.system.pt.BarSet;

public class TurtleAddAction {
	public static final int ADDTRADE_IN_TODAY_CLOSE = 1;
	public static final int ADDTRADE_IN_TOMORROW_OPEN = 2;
	public static final int ADDTRADE_OUT_TOMORROW_OPEN = 3;
	public static final int ADDTRADE_OUT_TOMORROW_CLOSE = 4;

	public static ArrayList<TurtleAddAction> allActions = new ArrayList<TurtleAddAction>();
	public Bar todayBar;
	public Bar todayBar2;
	Bar tomorrowBar;
	Bar tomorrowBar2;
	int addInTime;
	int addOutTime;
	int addLongOrShort;
	int todayIndex;
	private TurtleAction associatedBasedAction;

	private boolean isDiffTrade = false;
	public TurtleAddAction(BarSet barset, BarSet barset2, int todayIndex) {
		this.todayIndex = todayIndex;
		todayBar = barset.getBar(todayIndex);
		todayBar2 = barset2.getBar(todayIndex);
		tomorrowBar = barset.getBar(todayIndex + 1);
		tomorrowBar2 = barset2.getBar(todayIndex + 1);
		allActions.add(this);
	}
	public void setADDData(int addInTime, int addOutTime, int longOrShort,
			TurtleAction associatedBasedAction) {
		this.addInTime = addInTime;
		this.addOutTime = addOutTime;
		this.addLongOrShort = longOrShort;
		this.associatedBasedAction = associatedBasedAction;
		isDiffTrade = false;
	}
	public void setDiffTradeADDData(int addInTime, int addOutTime, int longOrShort) {
		this.addInTime = addInTime;
		this.addOutTime = addOutTime;
		this.addLongOrShort = longOrShort;
		this.associatedBasedAction = null;
		isDiffTrade = true;
	}
	
	public String getAdditionActionMessage() {
		double addInPrice = 0;
		double addOutPrice = 0;
		StringBuffer buf = new StringBuffer();
		
		if (associatedBasedAction == null || associatedBasedAction.inOrOut == TurtleAction.IN_ACTION) {
			if (this.addInTime == ADDTRADE_IN_TODAY_CLOSE) {
				buf.append(todayBar.mergeBar(todayBar2) + "\n");
				buf.append("收盤進場, ");
				addInPrice = todayBar2.close;
			} else {
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				buf.append("開盤進場, ");
				addInPrice = tomorrowBar2.open;
			}
			if (associatedBasedAction == null) {
				if (isDiffTrade == false) {
				  buf.append("進場作空, 雙向突破日作空,");
				}
				else {
					buf
					.append("期貨價過"
							+ (this.addLongOrShort == TurtleAction.LONG_ACTION ? "低"
									: "高")
							+ "於現貨價,");
				}
			}
			else 	if (this.todayIndex > associatedBasedAction.todayIndex) {
				buf
						.append("進場作"
								+ (associatedBasedAction.longOrShort == TurtleAction.LONG_ACTION ? "多"
										: "空")
								+ "第"
								+ (this.todayIndex - associatedBasedAction.todayIndex)
								+ "天無隔日出場訊號,");
			} else {
				buf
						.append("進場訊號加碼進場"
								+ ", 理由："
								+ "前一日創 "
								+ ((associatedBasedAction.longOrShort == TurtleAction.LONG_ACTION || associatedBasedAction.longOrShort == TurtleAction.LONG_ACTION_KEEP_PREV_TRADE_OR_ADVANCED_IN) ? (associatedBasedAction.daysIn4Long + " 天新高")
										: (associatedBasedAction.daysIn4Short + " 天新低"))
								+ ", ");
			}
			buf.append(String.format("前一日大盤[%.0f-%.0f-%.0f-%.0f-diff=%.0f], ",
					todayBar.open, todayBar.high, todayBar.low, todayBar.close,
					todayBar.diff));
			buf
					.append("做"
							+ ((this.addLongOrShort == TurtleAction.LONG_ACTION || this.addLongOrShort == TurtleAction.LONG_ACTION_KEEP_PREV_TRADE_OR_ADVANCED_IN) ? "多"
									+ ", 進場價 "
									+ String.format("%.0f", addInPrice)
									+ " (停損價:"
									+ String
											.format(
													"%.0f",
													addInPrice
															- TurtleAction.HALF_FORCE_OUT_VALUE)
									+ ")"

									: "空"
											+ ", 進場價 "
											+ String.format("%.0f", addInPrice)
											+ " (停損價:"
											+ String
													.format(
															"%.0f",
															addInPrice
																	+ TurtleAction.HALF_FORCE_OUT_VALUE)
											+ ")"));

			buf.append("\n");

			/* 需考慮強制停損! */
			if (this.addOutTime != ADDTRADE_OUT_TOMORROW_OPEN
					&& addLongOrShort == TurtleAction.LONG_ACTION
					&& addInPrice - tomorrowBar2.low > TurtleAction.HALF_FORCE_OUT_VALUE) {
				//addOutPrice = addInPrice - TurtleAction.HALF_FORCE_OUT_VALUE;
				if (addInPrice - tomorrowBar2.open  > TurtleAction.HALF_FORCE_OUT_VALUE) {
					addOutPrice = tomorrowBar2.open;
				} else {
					addOutPrice = addInPrice
							- TurtleAction.HALF_FORCE_OUT_VALUE;
				}
				
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				buf.append("強制出場, 進場訊號加碼出場, 出場價 "
						+ String.format("%.0f", addOutPrice) + ", 理由："
						+ "盤中強制停損" + TurtleAction.HALF_FORCE_OUT_VALUE + "出場,");

			} else if (this.addOutTime != ADDTRADE_OUT_TOMORROW_OPEN
					&& addLongOrShort == TurtleAction.SHORT_ACTION
					&& tomorrowBar2.high - addInPrice > TurtleAction.HALF_FORCE_OUT_VALUE) {
				if (tomorrowBar2.open - addInPrice > TurtleAction.HALF_FORCE_OUT_VALUE) {
					addOutPrice = tomorrowBar2.open;
				} else {
					addOutPrice = addInPrice
							+ TurtleAction.HALF_FORCE_OUT_VALUE;
				}
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				buf.append("強制出場, 進場訊號加碼出場, 出場價 "
						+ String.format("%.0f", addOutPrice) + ", 理由："
						+ "盤中強制停損" + TurtleAction.HALF_FORCE_OUT_VALUE + "出場,");
			} else {

				if (this.addOutTime == ADDTRADE_OUT_TOMORROW_OPEN) {
					buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
					buf.append("開盤出場");
					addOutPrice = tomorrowBar2.open;
				} else if (this.addOutTime == ADDTRADE_OUT_TOMORROW_CLOSE) {
					buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
					buf.append("收盤出場");
					addOutPrice = tomorrowBar2.close;
				}

				buf.append("進場訊號加碼出場, ");

				buf.append(("出場, 出場價 " + String.format("%.0f", addOutPrice)));
			}
			double profit = this.addLongOrShort == TurtleAction.LONG_ACTION ? addOutPrice
					- addInPrice
					: addInPrice - addOutPrice;
			buf.append(" 獲利 * * * [" + String.format("%.0f]", profit));

		} else {
			if (this.addInTime == ADDTRADE_IN_TODAY_CLOSE) {
				buf.append(todayBar.mergeBar(todayBar2) + "\n");
				buf.append("收盤進場, ");
				addInPrice = todayBar2.close;
			} else {
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				buf.append("開盤進場, ");
				addInPrice = tomorrowBar2.open;
			}

			buf.append("出場訊號加碼進場" + ", 理由：");

			switch (associatedBasedAction.reason) {
			case TurtleAction.TREND_REVERT_REASON:
				buf
						.append((associatedBasedAction.longOrShort == TurtleAction.LONG_ACTION ? ("前一日創 "
								+ associatedBasedAction.daysOut4Long + " 天新高")
								: ("前一日創 "
										+ associatedBasedAction.daysOut4Short + " 天新低"))
								+ ", ");
				break;
			case TurtleAction.STOP_LOSS_REASON:
				buf
						.append((associatedBasedAction.longOrShort == TurtleAction.LONG_ACTION ? ("多單停損 "
								+ associatedBasedAction.stopLoss4Long + " 點    ")
								: ("空單停損"
										+ associatedBasedAction.stopLoss4Short + " 點    "))
								+ ", ");
				break;
			case TurtleAction.STOP_PROFIT_REASON:
				buf
						.append((associatedBasedAction.longOrShort == TurtleAction.LONG_ACTION ? ("多單停利 "
								+ associatedBasedAction.stopProfit4Long + " 點    ")
								: ("空單停利"
										+ associatedBasedAction.stopProfit4Short + " 點    "))
								+ ", ");
				break;
			default:
				assert (true); /* currently not support */
				break;
			}
			buf.append(String.format("前一日大盤[%.0f-%.0f-%.0f-%.0f-diff=%.0f], ",
					todayBar.open, todayBar.high, todayBar.low, todayBar.close,
					todayBar.diff));
			buf
					.append("做"
							+ ((this.addLongOrShort == TurtleAction.LONG_ACTION) ? "多"
									+ ", 進場價 "
									+ String.format("%.0f", addInPrice)
									+ " (停損價:"
									+ String
											.format(
													"%.0f",
													addInPrice
															- TurtleAction.HALF_FORCE_OUT_VALUE)
									+ ")"

									: "空"
											+ ", 進場價 "
											+ String.format("%.0f", addInPrice)
											+ " (停損價:"
											+ String
													.format(
															"%.0f",
															addInPrice
																	+ TurtleAction.HALF_FORCE_OUT_VALUE)
											+ ")"));

			buf.append("\n");

			/* 需考慮強制停損! */
			if (this.addOutTime != ADDTRADE_OUT_TOMORROW_OPEN
					&& addLongOrShort == TurtleAction.LONG_ACTION
					&& addInPrice - tomorrowBar2.low > TurtleAction.HALF_FORCE_OUT_VALUE) {
				addOutPrice = addInPrice - TurtleAction.HALF_FORCE_OUT_VALUE;
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				buf.append("強制出場, 進場訊號加碼出場, 出場價 "
						+ String.format("%.0f", addOutPrice) + ", 理由："
						+ "盤中強制停損" + TurtleAction.HALF_FORCE_OUT_VALUE + "出場,");

			} else if (this.addOutTime != ADDTRADE_OUT_TOMORROW_OPEN
					&& addLongOrShort == TurtleAction.SHORT_ACTION
					&& tomorrowBar2.high - addInPrice > TurtleAction.HALF_FORCE_OUT_VALUE) {
				addOutPrice = addInPrice + TurtleAction.HALF_FORCE_OUT_VALUE;
				buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
				buf.append("強制出場, 進場訊號加碼出場, 出場價 "
						+ String.format("%.0f", addOutPrice) + ", 理由："
						+ "盤中強制停損" + TurtleAction.HALF_FORCE_OUT_VALUE + "出場,");
			} else {
				if (this.addOutTime == ADDTRADE_OUT_TOMORROW_OPEN) {
					buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
					buf.append("開盤出場");
					addOutPrice = tomorrowBar2.open;
				} else if (this.addOutTime == ADDTRADE_OUT_TOMORROW_CLOSE) {
					buf.append(tomorrowBar.mergeBar(tomorrowBar2) + "\n");
					buf.append("收盤出場");
					addOutPrice = tomorrowBar2.close;
				}

				buf.append("出場訊號加碼進場, ");

				buf.append(("出場, 出場價 " + String.format("%.0f", addOutPrice)));
			}
			double profit = this.addLongOrShort == TurtleAction.LONG_ACTION ? addOutPrice
					- addInPrice
					: addInPrice - addOutPrice;
			buf.append(" 獲利 * * * [" + String.format("%.0f]", profit));
		}

		//System.out.println(buf.toString());
		//System.out.println("===========================");

		return "***"+buf.toString();

	}
}
